package com.lanou.musicproject.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.lanou.musicproject.R;
import com.lanou.musicproject.adapter.SongListViewAdapter;

import com.lanou.musicproject.bean.Seek;
import com.lanou.musicproject.bean.Song;
import com.lanou.musicproject.utils.MyConfig;
import com.lanou.musicproject.volley.VolleySingleton;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.sso.UMSsoHandler;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by dllo on 15/10/22.
 * 搜索页面中的歌曲页面
 */
public class SongFragment extends Fragment  {
    private String url;
    private ListView song_listview;
    private VolleySingleton volleySingleton;
    private List<Song.DataEntity> data;
    private SongListViewAdapter adapter;
    private UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.songfragment, null);
        song_listview = (ListView) view.findViewById(R.id.song_listview);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        initData();
        return view;
    }

    private void initData() {
        //接收数据替换q
        //得到缓存中的Seekcontent ,q值解析
        String q = MyConfig.getSharePreStr(getActivity(), "Seekcontent", "url");
        Log.i("dsadsaddasdasdsadsadsa","++++++++++++++"+q);

        url = "http://api.dongting.com/misc/search/song?uid=864892023797876&hid=3112728442859043&" +
                "from=android&resolution=720x1280&net=2&api_version=1.0&utdid=VJU5DHOX3d4DAE1KKtjt%2BPXO&page=" +
                "1&longitude=121.5448&user_id=0&splus=4.3%252F18&tid=0&client_id=9eddc0eb843857fb2081a812f7645dc0&f=" +
                "f0&os=4.3&app=ttpod&rom=OPPO%252FR7007%252FR7007%253A4.3%252FJLS36C%252F1390465867%253Auser%252Frelease-keys" +
                "&alf=alf10001791&imei=864892023797876&" +
                "cpu=msm8226&agent=none&size=50&v=v8.2.0.2015091720&s=s200&address=" +
                "%E8%BE%BD%E5%AE%81%E7%9C%81%E5%A4%A7%E8%BF%9E%E5%B8%82%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA%E4%BA%94%E4%B8%80%E8%B7%AF314" +
                "&ram=870432+kB&q=" +
                q + //改变数据
                "&active=0&latitude=38.883507&language=zh&" +
                "imsi=460004261287923&mid=R7007&cpu_model=Qualcomm+MSM+8226+%28Flattened+Device+Tree%29";
        JsonObjectRequest jr = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                String str = response.toString();
                Song song = JSON.parseObject(str, Song.class);
                data = song.getData();
                adapter = new SongListViewAdapter(getActivity(), data);
                song_listview.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(jr);
    }
    public  void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**使用SSO授权必须添加如下代码 */
        UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode) ;
        if(ssoHandler != null){
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }


}
